package com.lanswon.icc.common.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.CharsetUtil;
import com.lanswon.icc.common.core.constant.ResultConstant;
import com.lanswon.icc.common.core.exception.CheckedException;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * @Description
 * @Author zsw
 * @Date 2019/10/28 17:31
 * @Version V1.0
 **/
@UtilityClass
public class WebUtils extends org.springframework.web.util.WebUtils {

    private final String BASIC_ = "Basic ";
    private final String UNKNOWN = "unknown";

    public String getIP(HttpServletRequest request) {
        Assert.notNull(request, "HttpServletRequest is null");
        String ip = request.getHeader("X-Requested-For");
        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return StringUtils.isBlank(ip) ? null : ip.split(",")[0];
    }

    /**
     * @Author zsw
     * @Description  从request 获取CLIENT_ID
     * @Date 2019/10/28 17:33 
     * @param request 
     * @return java.lang.String[] 
     */
    @SneakyThrows
    public String[] getClientId(ServerHttpRequest request)throws Exception {
        String header = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);

        if (header == null || !header.startsWith(BASIC_)) {
            throw new CheckedException(ResultConstant.EX_DATA_IS_NULL,"请求头中client信息为空");
        }
        byte[] base64Token = header.substring(6).getBytes(CharsetUtil.UTF_8);
        byte[] decoded;
        try {
            decoded = Base64.decode(base64Token);
        } catch (IllegalArgumentException e) {
            throw new CheckedException(ResultConstant.EX_ERROR_TOKEN,
                    "解析 [basic authentication] 失败");
        }

        String token = new String(decoded, StandardCharsets.UTF_8);

        int delim = token.indexOf(":");

        if (delim == -1) {
            throw new CheckedException(ResultConstant.EX_ERROR_TOKEN,"错误的 [ basic authentication ]");
        }
        return new String[]{token.substring(0, delim), token.substring(delim + 1)};
    }


    public static void main(String[] args) {


//        String encode = Base64.encode("admin:123456");
//        System.out.println(encode);


        byte[] decode = Base64.decode("YWRtaW46MTIzNDU3");
        String token = new String(decode, StandardCharsets.UTF_8);
        System.out.println(token);
    }


}